from django.shortcuts import render


def handle_exception_middleware(get_resp):
    """处理全局异常的中间件"""

    def wrapper(request, *args, **kwargs):
        try:
            resp = get_resp(request, *args, **kwargs)
            if resp.status_code >= 500:
                resp = render(request, '404.html')
        except Exception:
            resp = render(request, '404.html')
        return resp

    return wrapper
